package com.webservice.impl;

import com.dao.EquipmentDao;
import com.dao.UserDao;
import com.domain.Equipment;
import com.domain.Equipment_type;
import com.domain.User;
import com.webservice.EquipmentWebservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import java.util.ArrayList;
import java.util.List;

@WebService(targetNamespace = "http://39.98.59.6/mmms_test/ws/equipment")
public class EquipmentWebserviceImpl implements EquipmentWebservice {

    @Autowired
    private EquipmentDao equipmentDao;

    @Autowired
    private UserDao userDao;

    @Override
    public int addEquipment(@WebParam(name = "equipment") Equipment equipment) {
        if(equipment != null){
            equipmentDao.addEquipment(equipment);
            return 0;
        }
        else {
            return -1;
        }

    }


    /**
     * 根据单位查询所有设备
     * @param insiderUnit_id
     * @return
     */
    @WebResult(name = "EquipmentList")
    @Override
    public List<Equipment> findAllEquipmentByInsiderUnit(@WebParam(name = "insiderUnit_id") Integer insiderUnit_id){
        List<User> userList = userDao.findUserByInsiderUnit(insiderUnit_id);
        List<Equipment> equipmentList = new ArrayList<>();
        if (userList!=null){
            for (User user:userList){
                List<Equipment> equipmentList1 = equipmentDao.findByUser_id(user.getId());
                if (equipmentList1!=null){
                    equipmentList.addAll(equipmentList1);
                }
            }
        }
        return equipmentList;
    }

    @WebResult(name = "EquipmentList")
    @Override
    public List<Equipment> findByUser_id(@WebParam(name = "user_id")Integer user_id) {
        List<Equipment> equipmentList = equipmentDao.findByUser_id(user_id);
        return equipmentList;
    }

    @WebResult(name = "Equipment")
    @Override
    public Equipment findById(@WebParam(name = "equipment_id")Integer equipment_id) {
        Equipment equipment = equipmentDao.findOneById(equipment_id);
        return equipment;
    }

    @WebResult(name = "int")
    @Override
    public int updateEquipment(@WebParam(name = "equipment")Equipment equipment) {
        if (equipment == null || equipment.getEquipment_id() == null && equipmentDao.findOneById(equipment.getEquipment_id()) == null){
            return -1;
        }
        Equipment equipment1 = equipmentDao.findOneById(equipment.getEquipment_id());
        if (StringUtils.isEmpty(equipment.getEquipment_photos()) && !StringUtils.isEmpty(equipment1.getEquipment_photos())){
            equipment.setEquipment_photos(equipment1.getEquipment_photos());
        }
        equipment.setUser_id(equipment1.getUser_id());
        equipment.setMaintenance_times(equipment.getMaintenance_times());
        equipmentDao.updateEquipment(equipment);
        return 0;

    }

    @Override
    public void delEquipment(@WebParam(name = "id")Integer id) {
        equipmentDao.delEquipment(id);
    }

    @Override
    public Float priceEquipment(@WebParam(name = "equipment_id")Integer equipment_id,
                                @WebParam(name = "time")String time) {
        Float price = equipmentDao.priceEquipment(equipment_id, time);
        return price;
    }

    @Override
    public List<Equipment_type> findAllType() {
        List<Equipment_type> equipment_typeList = equipmentDao.findAllType();
        return equipment_typeList;
    }

    @Override
    public List<Equipment> findAllEquipment() {
        List<Equipment> equipmentList = equipmentDao.findAllEquipment();
        return equipmentList;
    }
}
